<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="房间位置" name="buildingId">
                        <a-input v-model:value="searchFormState.buildingId" placeholder="请输入房间位置" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="房间名称" name="roomName">
                        <a-input v-model:value="searchFormState.roomName" placeholder="请输入房间名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="状态" name="status">
                        <a-input v-model:value="searchFormState.status" placeholder="请输入状态" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="容纳人数" name="galleryful">
                        <a-input v-model:value="searchFormState.galleryful" placeholder="请输入容纳人数" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="设备类型" name="equipmentTypeId">
                        <a-input v-model:value="searchFormState.equipmentTypeId" placeholder="请输入设备类型" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="使用审批人" name="approver">
                        <a-input v-model:value="searchFormState.approver" placeholder="请输入使用审批人" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="责任人" name="dutyOfficer">
                        <a-input v-model:value="searchFormState.dutyOfficer" placeholder="请输入责任人" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="房间类型" name="roomType">
                        <a-input v-model:value="searchFormState.roomType" placeholder="请输入房间类型" />
                    </a-form-item>
                </a-col>
                <a-col :span="6" v-show="advanced">
                    <a-form-item label="是否涉密" name="isSecret">
                        <a-input v-model:value="searchFormState.isSecret" placeholder="请输入是否涉密" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="reset">重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                        {{ advanced ? '收起' : '展开' }}
                        <component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
                    </a>
                </a-col>
            </a-row>
        </a-form>
        <s-table
            ref="tableRef"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.id"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizRoomInfoAdd')">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
					<a-button @click="exportBatchRoomVerify">
						<template #icon><export-outlined /></template>
						{{ $t('user.batchExportButton') }}
					</a-button>
                    <xn-batch-delete
                        v-if="hasPerm('bizRoomInfoBatchDelete')"
                        :selectedRowKeys="selectedRowKeys"
                        @batchDelete="deleteBatchBizRoomInfo"
                    />
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'isSecret'">
					{{ $TOOL.dictTypeData('COMMON_WHETHER', record.isSecret) }}
				</template>
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('bizRoomInfoEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['bizRoomInfoEdit', 'bizRoomInfoDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteBizRoomInfo(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('bizRoomInfoDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="roominfo">
    import { cloneDeep } from 'lodash-es'
    import Form from './form.vue'
    import bizRoomInfoApi from '@/api/biz/bizRoomInfoApi'
	import downloadUtil from '@/utils/downloadUtil'
	import { message, Empty } from 'ant-design-vue'
    const searchFormState = ref({})
    const searchFormRef = ref()
    const tableRef = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    // 查询区域显示更多控制
    const advanced = ref(false)
    const toggleAdvanced = () => {
        advanced.value = !advanced.value
    }
    const columns = [
        {
            title: '房间位置',
            dataIndex: 'buildingName'
        },
        {
            title: '房间名称',
            dataIndex: 'roomName'
        },
        {
            title: '状态',
            dataIndex: 'status'
        },
        {
            title: '容纳人数',
            dataIndex: 'galleryful'
        },
        {
            title: '设备类型',
            dataIndex: 'equipmentTypeName'
        },
        {
            title: '使用审批人',
            dataIndex: 'approverName'
        },
        {
            title: '责任人',
            dataIndex: 'dutyOfficerName'
        },
        {
            title: '服务人员',
            dataIndex: 'serviceStaffName'
        },
        {
            title: '房间类型',
            dataIndex: 'roomTypeName'
        },
        {
            title: '是否涉密',
            dataIndex: 'isSecret'
        },
        {
            title: '使用部门',
            dataIndex: 'usedOrgName'
        },
        {
            title: '管理部门',
            dataIndex: 'manageOrgName'
        },
        {
            title: '房间二维码',
            dataIndex: 'rootQr'
        },
    ]
    // 操作栏通过权限判断是否显示
    if (hasPerm(['bizRoomInfoEdit', 'bizRoomInfoDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }
    const selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        // columns数字类型字段加入 needTotal: true 可以勾选自动算账
        alert: {
            show: true,
            clear: () => {
                selectedRowKeys.value = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = cloneDeep(searchFormState.value)
        return bizRoomInfoApi.bizRoomInfoPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
    // 重置
    const reset = () => {
        searchFormRef.value.resetFields()
        tableRef.value.refresh(true)
    }
    // 删除
    const deleteBizRoomInfo = (record) => {
        let params = [
            {
                id: record.id
            }
        ]
        bizRoomInfoApi.bizRoomInfoDelete(params).then(() => {
            tableRef.value.refresh(true)
        })
    }
	// 批量导出校验并加参数
	const exportBatchRoomVerify = () => {
		debugger
		if ((selectedRowKeys.value.length < 1) && !searchFormState.value.searchKey ) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				ids: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			exportBatchRoom(params)
			return
		}
		if (searchFormState.value.searchKey ) {
			const params = {
				searchKey: searchFormState.value.searchKey,
			}
			exportBatchRoom(params)
		}
	}
	// 批量导出
	const exportBatchRoom = (params) => {
		bizRoomInfoApi.bizRoomInfoExport(params).then((res) => {
			downloadUtil.resultDownload(res)
			tableRef.value.clearSelected()
		})
	}
    // 批量删除
    const deleteBatchBizRoomInfo = (params) => {
        bizRoomInfoApi.bizRoomInfoDelete(params).then(() => {
            tableRef.value.clearRefreshSelected()
        })
    }
</script>
